package demo.controller;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import demo.service.ModelService;
import demo.service.entity.Model;

@Controller
@RequestMapping("Home")
public class Home {
	private ModelService modelService;
	@Autowired
	public Home(ModelService modelService) {
		this.modelService = modelService;
	}

	@RequestMapping("index")
	public ModelAndView index(@RequestParam("MileageID")int mileageID) {
		return new ModelAndView("Home/index","mileageID", mileageID);
	}
	
	@RequestMapping("modelObjectTree")
	public ModelAndView modelObjectTree(String type) {
		if (StringUtils.isEmpty(type)) {
			type="tree";
		}
		Model model;
		switch (type) {
		case "group":
			model = modelService.getModelWithChildrenInGroup(null);
			break;
		case "type":
			model = modelService.getModelWithChildrenInType(null);
			break;
		default:
			model = modelService.getModelWithChildrenInTree(null);
			break;
		}
		return new ModelAndView("Home/modelObjectTree","rootModel", model);
	}
	
	@RequestMapping("test")
	public ResponseEntity<String> test() {
		return new ResponseEntity<>("test", HttpStatus.OK);
	}
}
